package com.mall.mymall.common;

import java.io.Serializable;

/**
 * 通用返回结果类
 *
 * @param <T> 返回数据的类型
 * @author 十里坡
 * @since 2025-03-06
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    // 状态码
    private Integer code;

    // 消息
    private String message;

    // 返回的数据
    private T data;

    // 默认成功状态码
    public static final int SUCCESS_CODE = 200;

    // 默认失败状态码
    public static final int FAIL_CODE = 500;

    // 默认成功消息
    public static final String SUCCESS_MESSAGE = "操作成功";

    // 默认失败消息
    public static final String FAIL_MESSAGE = "操作失败";

    // 构造方法
    public Result() {
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 成功的静态方法
    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static <T> Result<T> success(T data, String message) {
        return new Result<>(SUCCESS_CODE, message, data);
    }

    // 失败的静态方法
    public static <T> Result<T> fail(String message) {
        return new Result<>(FAIL_CODE, message, null);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        return new Result<>(code, message, null);
    }

    // Getter和Setter方法
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}